<template>
  <div>
    <Dialogs v-model="dialog">
      <div class="body">
        <input class="form-control input-lg" type="text" @keyup.enter="setCode" v-model="code">
        <p class="tip" style="color:red">{{errcode}}</p>
      </div>
      <div slot="foot">
        <a class="lp-btn large primary" @click="setCode">确定</a>
      </div>
    </Dialogs>
    <Alert v-if="alert.show" :info="alert.info" :recall="alert.recall" @setClose="value=>alert.show=value" />
  </div>
</template>

<script>
import Dialogs from '@/components/Dialog'
import Alert from '@/components/Alert'
import { post, get } from '@/functions/ajax'

export default {
  name: 'app',
  components: {
    Dialogs,
    Alert
  },
  data() {
    return {
      code: '',
      errcode: '',
      dialog: {
        show: true,
        title: '请输入服务点代码',
        maskClose: false,
        close: false
      },
      alert: {
        info: '',
        show: false,
        recall: () => {}
      }
    }
  },
  methods: {
    async getPosition(code) {
      if (!code) {
        this.alert.info = '服务点信息错误!'
        this.alert.show = true
        return
      }
      const { info, status } = await post('/pufa/machine/', {
        no: code
      })

      console.log(status)
      if (!status) {
        this.alert.info = '服务点信息错误!'
        this.alert.show = true
        return
      }
      // this.$setCookie('code', info.no, '365d')
      // this.$setCookie('servicePoit', JSON.stringify(info), '365d')

      // if (!this.$getCookie('servicePoit') || !this.$getCookie('code')) return
      window.localStorage.setItem('code', info.no)
      // window.sessionStorage.setItem('servicePoit', JSON.stringify(info))
      // window.localStorage.setItem('servicePoit', JSON.stringify(info))
      // console.log(localStorage)
      // if (!localStorage.code || !sessionStorage.servicePoit) return

      this.$root.point = info

      const res = await get('/get/website/info/?code=' + info.code)
      if (!res.status) {
        this.alert.info = res.msg
        this.alert.show = true
        return
      }
      this.$root.config = res.result.info
      this.$root.area = res.result.area

      this.errcode = ''
      this.dialog.show = false

      // console.log(this.$router.options.routes)
      this.$router.push('/')
      // this.$router.push('law')
    },
    setCode() {
      // test.hello('js调用了android中的hello方法')
      this.getPosition(this.code.trim())
    }
  }
}
</script>
